/*
 * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */
package org.mule.compatibility.config.spring.parsers.specific;

import static org.junit.Assert.assertEquals;

import org.mule.compatibility.core.api.endpoint.EndpointURI;
import org.mule.compatibility.core.endpoint.URIBuilder;
import org.mule.runtime.core.api.MuleContext;
import org.mule.tck.junit4.AbstractMuleTestCase;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

public class URIBuilderTestCase extends AbstractMuleTestCase {

  private static final Map queries;

  static {
    queries = new HashMap();
    queries.put("aname", "avalue");
    queries.put("bname", "bvalue");
  }

  private MuleContext unusedMuleContext = null;

  @Test
  public void testAddressForProtocol() {
    URIBuilder uri = new URIBuilder(unusedMuleContext);
    uri.setProtocol("foo");
    uri.setAddress("foo://bar");
    assertEquals("foo://bar", uri.toString());
  }

  @Test
  public void testAddressForMeta() {
    URIBuilder uri = new URIBuilder(unusedMuleContext);
    uri.setMeta("foo");
    uri.setAddress("baz://bar");
    assertEquals("foo:baz://bar", uri.toString());
  }

  @Test
  public void testQueriesWithAddress() {
    URIBuilder uri = new URIBuilder(unusedMuleContext);
    uri.setAddress("foo://bar");
    uri.setQueryMap(queries);
    assertEquals("foo://bar?aname=avalue&bname=bvalue", uri.toString());
  }

  @Test
  public void testLiteralQueries() {
    URIBuilder uri = new URIBuilder(unusedMuleContext);
    uri.setAddress("foo://bar?cname=cvalue");
    uri.setQueryMap(queries);
    assertEquals("foo://bar?cname=cvalue&aname=avalue&bname=bvalue", uri.toString());
  }

  @Test
  public void testFromString() {
    URIBuilder uri = new URIBuilder("test://bar", unusedMuleContext);
    EndpointURI endpointURI = uri.getEndpoint();
    assertEquals("test://bar", endpointURI.getUri().toString());
    assertEquals("test", endpointURI.getSchemeMetaInfo());
    uri = new URIBuilder("meta:test://bar", unusedMuleContext);
    endpointURI = uri.getEndpoint();
    assertEquals("test://bar", endpointURI.getUri().toString());
    assertEquals("meta", endpointURI.getSchemeMetaInfo());
  }

}
